//注册中心的web service
package main

import (
	"FenBuShiProject/registry"
	"context"
	"fmt"
	"log"
	"net/http"
)

func main() {
	//开启心跳
	registry.SetupRegistryService()
	http.Handle("/services", &registry.RegistryService{})
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	var srv http.Server
	srv.Addr = registry.Serverport

	go func() {
		log.Println(srv.ListenAndServe())
		cancel()
	}()

	go func() {
		fmt.Printf("Registry started.Press any key to stop. \n")
		var s string
		fmt.Scanln(&s)
		//Shutdown(ctx) 除了等待活跃连接的同时也会监听 Context 完成事件，二者有一个触发都会触发程序终止
		srv.Shutdown(ctx) //ctx是为了cancel控制
		cancel()
	}()

	<-ctx.Done()
	fmt.Println("Shutting down registry service")
}
